package com.example.hospital.util;


import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;
import static org.fusesource.jansi.Ansi.Color.*;

import java.io.Console;
import java.util.Scanner;
/*辅助工具类*/
public class ConsoleUtils {

    private static Scanner scanner = new Scanner(System.in);

    static {
        //利用ANSI转义代码，来控制终端输出
        AnsiConsole.systemInstall();
    }

    public static void printColored(String message, Ansi.Color color) {
        System.out.println(Ansi.ansi().fg(color).a(message).reset());
    }


    public static String readLine(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    // 该方法以隐藏密码输入
    public static String readPassword(String prompt) {
        Console console = System.console();
        if (console != null) {
            char[] passwordChars = console.readPassword(prompt);
            return new String(passwordChars);
        } else {
            // 如果无法获取 Console（如在 IDE 中运行），回退到普通输入
            //printColored("无法隐藏密码输入，密码将以明文显示。", RED);
            return readLine(prompt);
        }
    }

    public static int readInt(String prompt) {
        while (true) {
            try {
                String input = readLine(prompt);
                return Integer.parseInt(input);
            } catch (NumberFormatException e) {
                printColored("请输入有效的整数！", RED);
            }
        }
    }

    public static void pause() {
        System.out.println("按回车键继续...");
        scanner.nextLine();
    }
}
